package me.ahoukuse.clipboard;

import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;

public class ClipboardHelper {
    public static String getClipBoardText(Context context) {
        ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        assert manager != null;
        ClipData data = manager.getPrimaryClip();
        if (data != null){
            if (data.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)){
                CharSequence charSequence = data.getItemAt(0).getText();
                return charSequence.toString();
            }
        }
        return null;
    }

    public static void setClipboardText(Context context,String text){
        ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = ClipData.newPlainText("Sync",text);
        assert clipboardManager != null;
        clipboardManager.setPrimaryClip(clipData);
    }
}
